#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import scrapy
import sys
from datetime import datetime
from gupiao.items import GupiaoItem
from gupiao.mypkg import twj9018

class ZgzcSpider(scrapy.Spider):

    # 中国中车A股
    name = 'zgzc'
    allowed_domains = ['qq.com']
    start_urls = ['http://gu.qq.com/sh601766/gp']

    def _report_warning(self, reason):

        _content = """
        Hi twj:

            Warning, Can not get the info of [{0}] from {1},
        Reason: [{2}].
        Please deal with the problem as soon as possible.

                                    From: aliyun by scrapy
                                    Date: {3}
        """.format(self.name, self.start_urls, reason,
                   datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

        _subject = "Can not get the info of [{0}]".format(self.name)
        twj9018.main(_subject, _content)

    def parse(self, response):
        try:
            _code = response.status
        except Exception:
            _code = '990'

        if int(_code) >= 400:
            self._report_warning('http with error code {0}'.format(_code))
            return

        _name = response.xpath('//*[@id="hqpanel"]/div[1]/div[1]/h1[1]/text()')
        price = response.xpath('//*[@id="spFP"]/div[1]/span[1]/text()')

        try:
            _name = _name[0].extract()
            price = price[0].extract()
        except Exception:
            self._report_warning('can not extract info')
            return
        else:
            if not _name or not price:
                self._report_warning('extracted None value')
                return

        _item = GupiaoItem()
        _item['name'] = _name
        _item['price'] = price

        yield _item
